/*
 * =====================================================================================
 * Copyright (C) 2024 Ingenic Semiconductor Co.,Ltd
 * All Rights Reserved
 *
 * Filename     : message.c
 * Author       : Keven <keven.ywhan@ingenic.com>
 * Created      : 2024/06/20 16:42
 * Description  :
 *
 * =====================================================================================
 */

#include "message.h"

void *init_mesg_queue(uint32_t msg_num, uint32_t msg_size)
{
	return (void *)xQueueCreate(msg_num, msg_size);
}

int32_t send_msg(void *msg_handle, void* msg_data)
{
	int status = 0;
	status = xQueueSendToBack((QueueHandle_t)msg_handle, msg_data, 0);
	if(status != pdPASS) {
		printk("Could not send to the queue\n");
		return -1;
	}
	return 0;
}

int32_t send_msg_isr(void *msg_handle, void* msg_data)
{
	int status = 0;
	BaseType_t xHigherPriorityTaskWoken = pdFALSE;

	status = xQueueSendToBackFromISR((QueueHandle_t)msg_handle, msg_data, &xHigherPriorityTaskWoken);
	if(status != pdPASS) {
		printk("Could not send to the queue\n");
		return -1;
	}
	return 0;
}

int32_t wait_msg(void *msg_handle, void* msg_data)
{
	int status = 0;
	status = xQueueReceive((QueueHandle_t)msg_handle, msg_data, portMAX_DELAY);
	if(status != pdPASS) {
		printk("Could not receive to the queue\n");
		return -1;
	}
	return 0;
}

int32_t wait_msg_timeout(void *msg_handle, void* msg_data, uint32_t timeout)
{
	int status = 0;
	status = xQueueReceive((QueueHandle_t)msg_handle, msg_data, timeout);
	if(status != pdPASS) {
		printk("Could not receive to the queue\n");
		return -1;
	}
	return 0;
}
